import unittest

# Python 提供名为 re 的内置包，可用于处理正则表达式。
import re

"""
 @author adun
 @date 2020/11/6 23:32
 
 RegEx 或正则表达式是形成搜索模式的字符序列。

 RegEx 可用于检查字符串是否包含指定的搜索模式。
"""

# 函数	描述
# findall	返回包含所有匹配项的列表
# search	如果字符串中的任意位置存在匹配，则返回 Match 对象
# split	返回在每次匹配时拆分字符串的列表
# sub	用字符串替换一个或多个匹配项

class MyTestCase(unittest.TestCase):
    def test_something(self):
        self.assertEqual(True, False)

    # 检索字符串以查看它是否以 "China" 开头并以 "country" 结尾：
    def test_start_end(self):
        txt = "China is a great country"
        x = re.search("^China.*country$", txt)
        print(type(x))
        print(x)
        if (x):
            print("YES! We have a match!")
        else:
            print("No match")


    # findall	返回包含所有匹配项的列表


 # 元字符
    # []	一组字符	"[a-m]"
    def test_findall_one(self):
        str = "The rain in Spain"

        # Find all lower case characters alphabetically between "a" and "m":

        x = re.findall("[a-m]", str)
        print(x)

    # \	示意特殊序列（也可用于转义特殊字符）	"\d"匹配0-9数字
    def test_match_number(self):
        str = "That will be 59 dollars"

        # Find all digit characters:

        x = re.findall("\d", str)
        print(x)

    # .	任何字符（换行符除外）	"he..o"
    def test_random_str(self):
        str = "hello world"

        # Search for a sequence that starts with "he", followed by two (any) characters, and an "o":

        x = re.findall("he..o", str)
        print(x)

    # ^	起始于	"^hello"        以什么字符为开头
    def test_start_str(self):
        str = "hello world"

        # Check if the string starts with 'hello':

        x = re.findall("^hello", str) # 以hello作为开始
        print(x)
        if (x):
            print("Yes, the string starts with 'hello'")
        else:
            print("No match")

    # $	结束于	"world$"  以什么字符结束
    def test_end_str(self):
        str = "hello world"

        # Check if the string ends with 'world':

        x = re.findall("world$", str) # 以world作为结束字符
        print(x)
        if (x):
            print("Yes, the string ends with 'world'")
        else:
            print("No match")

    # *	零次或多次出现	"aix*" 【零个或多个】
    def test_match_repeated(self):
        str = "The rain in Spain falls mainly in the plain!"

        # Check if the string contains "ai" followed by 0 or more "x" characters:

        x = re.findall("aix*", str) # x出现零次或多次

        print(x) # ['ai', 'ai', 'ai', 'ai']

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # +	一次或多次出现	"aix+" 【一个或多个】
    def test_zero_repeated(self):
        str = "The rain in Spain falls mainly in the plain!"

        # Check if the string contains "ai" followed by 1 or more "x" characters:

        x = re.findall("aix+", str) # x出现一次或多次

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # {}	确切地指定的出现次数	"al{2}"
    def test_num_match(self):
        str = "The rain in Spain falls mainly in the plain!"

        # Check if the string contains "a" followed by exactly two "l" characters:

        x = re.findall("al{2}", str) # 出现两次L

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # |	两者任一	"falls|stays"
    def test_falls_stays(self):
        str = "The rain in Spain falls stays mainly in the plain!"

        # Check if the string contains either "falls" or "stays":

        x = re.findall("falls|stays|main", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")
    # ()	捕获和分组

# 特殊序列指的是 \ 后跟下表中的某个字符，拥有特殊含义：
    # \A	如果指定的字符位于字符串的开头，则返回匹配项	"\AThe"
    def test_specified_character(self):
        str = "The rain in Spain"

        # Check if the string starts with "The":

        x = re.findall("\AThe", str)

        print(x)

        if (x):
            print("Yes, there is a match!")
        else:
            print("No match")

    # \b	返回指定字符位于单词的开头或末尾的匹配项	r"\bain"
    # r"ain\b"
    def test_start_end_str(self):
        str = "The rain in Spain"

        # Check if "ain" is present at the beginning of a WORD:

        x = re.findall(r"\bain", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")



        str = "The rain in Spain"

        # Check if "ain" is present at the end of a WORD:

        x = re.findall(r"ain\b", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")


    # \B	返回指定字符存在的匹配项，但不在单词的开头（或结尾处）	r"\Bain"
    # r"ain\B"
    def test_start_end_word(self):
        str = "The rain in Spain"

        # Check if "ain" is present, but NOT at the beginning of a word:

        x = re.findall(r"\Bain", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")





        str = "The rain in Spain"

        # Check if "ain" is present, but NOT at the end of a word:

        x = re.findall(r"ain\B", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")



    # \d	返回字符串包含数字的匹配项（数字 0-9）	"\d"
    def test_num(self):
        str = "The rain in Spain 88"

        # Check if the string contains any digits (numbers from 0-9):

        x = re.findall("\d", str)
        y=re.findall("[0-9][0-9]",str)

        print(x)
        print(y)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")


    # \D	返回字符串不包含数字的匹配项	"\D"
    def test_str_uncontain_str(self):
        str = "The rain in Spain"

        # Return a match at every no-digit character:

        x = re.findall("\D", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # \s	返回字符串包含空白字符的匹配项	"\s"
    def test_blank_str(self):
        str = "The rain in Spain"

        # Return a match at every white-space character:

        x = re.findall("\s", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # \S	返回字符串不包含空白字符的匹配项	"\S"
    def test_uncontain_blank(self):
        str = "The rain in Spain"

        # Return a match at every NON white-space character:

        x = re.findall("\S", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # \w	返回一个匹配项，其中字符串包含任何单词字符
    # （从 a 到 Z 的字符，从 0 到 9 的数字和下划线 _ 字符）	"\w"
    def test_contain_character(self):
        str = "The rain in Spain"

        # Return a match at every word character (characters from a to Z, digits from 0-9, and the underscore _ character):

        x = re.findall("\w", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # \W	返回一个匹配项，其中字符串不包含任何单词字符	"\W"
    def test_uncontains_character(self):
        str = "The rain in Spain"

        # Return a match at every NON word character (characters NOT between a and Z. Like "!", "?" white-space etc.):

        x = re.findall("\W", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # \Z	如果指定的字符位于字符串的末尾，则返回匹配项	"Spain\Z"
    def test_end_character(self):
        str = "The rain in Spain"

        # Check if the string ends with "Spain":

        x = re.findall("Spain\Z", str)

        print(x)

        if (x):
            print("Yes, there is a match!")
        else:
            print("No match")

# 集合（Set）
# 集合（Set）是一对方括号 [] 内的一组字符，具有特殊含义：

    # [arn]	返回一个匹配项，其中存在指定字符（a，r 或 n）之一
    def test_assignee_character(self):
        str = "The rain in Spain"

        # Check if the string has any a, r, or n characters:

        x = re.findall("[arn]", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # [a-n]	返回字母顺序 a 和 n 之间的任意小写字符匹配项
    def test_between_character(self):
        str = "The rain in Spain"

        # Check if the string has any characters between a and n:

        x = re.findall("[a-n]", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # [^arn]	返回除 a、r 和 n 之外的任意字符的匹配项
    def test_other_character(self):
        str = "The rain in Spain"

        # Check if the string has other characters than a, r, or n:

        x = re.findall("[^arn]", str)

        print(x) # 空格也算

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # [0123]	返回存在任何指定数字（0、1、2 或 3）的匹配项
    def test_assignee_num(self):
        str = "The rain in Spain"

        # Check if the string has any 0, 1, 2, or 3 digits:

        x = re.findall("[0123]", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # [0-9]	返回 0 与 9 之间任意数字的匹配
    def test_zero_to_nine(self):
        str = "8 times before 11:45 AM"

        # Check if the string has any digits:

        x = re.findall("[0-9]", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # [0-5][0-9]	返回介于 0 到 9 之间的任何数字的匹配项
    def test_tens(self):
        str = "8 times before 11:45 AM"

        # Check if the string has any two-digit numbers, from 00 to 59:

        x = re.findall("[0-5][0-9]", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")

    # [a-zA-Z]	返回字母顺序 a 和 z 之间的任何字符的匹配，小写或大写
    def test_contain_letter(self):
        str = "8 times before 11:45 AM"

        # Check if the string has any characters from a to z lower case, and A to Z upper case:

        x = re.findall("[a-zA-Z]", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")
    # [+]	在集合中，+、*、.、|、()、$、{} 没有特殊含义，因此 [+] 表示：返回字符串中任何 + 字符的匹配项
    def test_random_character(self):
        str = "8 times before 11:45 AM"

        # Check if the string has any + characters:

        x = re.findall("[+]", str)

        print(x)

        if (x):
            print("Yes, there is at least one match!")
        else:
            print("No match")


if __name__ == '__main__':
    unittest.main()
